package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageTenderListActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageTenderOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageTenderListBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class TenderInStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageTenderListBean.DataBean.TenderInStorageListBean> mTenderInStorageList;
    private LayoutInflater inflater;
    private InStorageTenderListActivity.InStorageTenderListener mInStorageTenderListener;
    private String mContractCode, mPurchase;

    public TenderInStorageListAdapter(Context context, List<InStorageTenderListBean.DataBean.TenderInStorageListBean> tenderInStorageList, String contractCode, String purchase, InStorageTenderListActivity.InStorageTenderListener inStorageTenderListener) {
        mContext = context;
        mTenderInStorageList = tenderInStorageList;
        mInStorageTenderListener = inStorageTenderListener;
        inflater = LayoutInflater.from(context);
        mContractCode = contractCode;
        mPurchase = purchase;
    }

    @Override
    public int getCount() {
        return mTenderInStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mTenderInStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_tender_list_first_item, null);
            EditText contractCode = (EditText) convertView.findViewById(R.id.contractCode);
            EditText purchase = (EditText) convertView.findViewById(R.id.purchase);
            Button search = (Button) convertView.findViewById(R.id.search);
            contractCode.setText(mContractCode);
            purchase.setText(mPurchase);
            contractCode.addTextChangedListener(new QRCodeEditTextWatcher(contractCode, true) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, contractCode);
                    purchase.requestFocus();
                }
            });
            purchase.addTextChangedListener(new QRCodeEditTextWatcher(purchase, true) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, purchase);
                    purchase.clearFocus();
                }
            });
            search.setOnClickListener(mInStorageTenderListener.getNoDoubleClickListener(contractCode, purchase));
        } else {
            InStorageTenderListBean.DataBean.TenderInStorageListBean tenderInstorageItem = mTenderInStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_tender_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, tenderInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.contractCode = (TextView) view.findViewById(R.id.contractCode);
        viewHolder.purchaseOrder = (TextView) view.findViewById(R.id.purchaseOrder);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageTenderListBean.DataBean.TenderInStorageListBean tenderInstorageItem) {
        viewHolder.contractCode.setText(tenderInstorageItem.getContractCode());
        viewHolder.purchaseOrder.setText(tenderInstorageItem.getPurchase());
        viewHolder.supplier.setText(tenderInstorageItem.getSupplier());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageTenderOverviewActivity.class);
                intent.putExtra(InStorageExtra.PURCHASE_ORDER, viewHolder.purchaseOrder.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView contractCode, purchaseOrder, supplier;
    }
}
